package com.csproj.siiat.base.common.logback;


import com.csproj.siiat.base.common.service.SendMailService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by wangjf on 2018/2/11.
 */
@Service
public class Logback {

    @Autowired
    private static SendMailService sendMailService;

    /**
     * warn级别，主要打印系统正常，但存在数据错误的日志报告
     */
    @Async
    public static  void warn (String className, String methodName, String lineNum,
                              String params, String remark, String creator, Logger logger){
        StringBuffer warn = new StringBuffer();
        warn.append("\nClassName=["+className+"."+methodName+"()/"+lineNum+"]");
        warn.append("\nParams=["+params+"]");
        warn.append("\nremark=["+remark+"]");
        warn.append("\ncreator=["+creator+"]");
        logger.warn(warn+"\n");
    }
    /**
     * warn级别，主要打印系统正常，但存在数据错误的日志报告
     */
    @Async
    public static  void warnAndSendMail(String params, String remark, String creator,Exception exception, Logger logger){
        StringBuffer warn = new StringBuffer();
        warn.append("\nParams=["+params+"]");
        warn.append("\nremark=["+remark+"]");
        warn.append("\ncreator=["+creator+"]");
        logger.warn(warn+"\n");

        if(exception != null) {
            StringBuffer warnBase = new StringBuffer();
            warnBase.append("<br />Params=["+params+"]");
            warnBase.append("<br />remark=["+remark+"]");
            warnBase.append("<br />creator=["+creator+"]");

            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            exception.printStackTrace(printWriter);
            sendMailService.sendErrorLogMail(warnBase.toString()+stringWriter.toString());
        }else{
            StringBuffer warnBase = new StringBuffer();
            warnBase.append("<br />Params=["+params+"]");
            warnBase.append("<br />remark=["+remark+"]");
            warnBase.append("<br />creator=["+creator+"]");
            sendMailService.sendErrorLogMail(warnBase.toString());
        }
    }

    /**
     * warn级别，主要打印系统正常，但存在数据错误的日志报告
     */
    @Async
    public static  void warn (String params, String remark, String creator, Logger logger){
        StringBuffer warn = new StringBuffer();
        warn.append("\nParams=["+params+"]");
        warn.append("\nremark=["+remark+"]");
        warn.append("\ncreator=["+creator+"]");
        logger.warn(warn+"\n");
    }

    /**
     * info级别信息打印
     */
    @Async
    public static  void info (String errMsg, Logger logger){
        long id = Thread.currentThread().getId();
        logger.info(errMsg+"\n");
    }

    /**
     * 异常信息存储2
     */
    @Async
    public static  void error(String msg, Exception e, Logger logger){
        logger.error(msg, e);
    }

    /**
     * error级别信息打印
     */
    @Async
    public static  void error (String errMsg, Logger logger){
        logger.error(errMsg+"\n");
    }

    /**
     * error级别信息打印
     */
    @Async
    public static  void error (Exception e, Logger logger){
        logger.error("", e);
    }
}
